<template>
	<view>
		<web-view :webview-styles="webviewStyles" :src="src" @message="getMessage" :update-title="false"></web-view>
	</view>
</template>

<script>
	import {
		shareFriend,
		shareMoment,
		saveAlbum
	} from '../../utility/share.js'
	export default {
		data() {
			return {
				webviewStyles: {
					progress: {
						color: '#FF8E2A'
					}
				},
				src: '',
				title: ''
			}
		},
		onLoad(option) {
			this.src = decodeURIComponent(option.url)
			this.title = option.title
		},
		onReady() {
			// #ifdef APP-PLUS
			if (this.title) {
				uni.setNavigationBarTitle({
					title: '一起种艾草'
				})
			}
			// #endif
		},
		methods: {
			getMessage(data) {
				const {action, invitationCode} = data.detail.data[0]
				if (action) {
					uni.showActionSheet({
						itemList: ['分享给朋友', '分享到朋友圈', '保存到相册', '复制邀请码'],
						success: function(res) {
							console.log(res)
							switch (res.tapIndex) {
								case 0:
									shareFriend(action)
									break;
								case 1:
									shareMoment(action)
									break;
								case 2:
									saveAlbum(action)
									break;
								case 3:
									uni.setClipboardData({
										data: invitationCode,
										success(res) {
											uni.showToast({
												title: '复制成功',
												icon: 'none'
											})
										},
										fail(err) {
											console.log(err)
											uni.showToast({
												title: '复制失败',
												icon: 'none'
											})
										}
									})
									break;
								default:
									break;
							}
						},
					})
				} else {
					uni.showToast({
						title: '暂未获得图片地址',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style>
</style>
